<?php
namespace app\index\controller;

use think\Config;
use think\Db;

class Trade
{
    public function index(){
        $re = array(
            'status' => 400,
            "msg" => "查询失败",
        );
        $outTradeNo=$_REQUEST["out_trade_no"];
        $pay_re = Db::table('ims_zh_pay_order')->where(array('out_trade_no'=>$outTradeNo))->order('id desc')->find();//查询订单记录
        if($pay_re){
            if($pay_re['pay_way'] == 1){
                $re=$this->aliPay($outTradeNo);
            }elseif($pay_re['pay_way'] == 2){
                $re=$this->wxPay($outTradeNo);
            }
        }
        print_r($re);exit();
    }

    public function aliPay($out_trade_no)
    {
        vendor("alipay.AopSdk");

        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config('pay.ali_appid');
        $aop->rsaPrivateKey = config('pay.ali_key');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayTradeQueryRequest();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"".$out_trade_no ."\"," .
            "\"trade_no\":\"\"," .
            "\"org_pid\":\"\"" .
            "  }"); //CTV15373465429412
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            $re=array(
                'status'=>200,
                "msg" => "请求成功!",
                "data"=>array(
                    "out_trade_no"=> $_GET['out_trade_no'],
                    "alipay_trade_status"=>"WAIT_BUYER_PAY"
                )
            );
            return json_encode($re);
        } else {
            $re=array(
                'status'=>400,
                "msg" => "查询失败",
            );
            return json_encode($re);
        }

    }

    public function wxPay($out_trade_no)
    {
        vendor("wxPayReturn.WxPay_Config");
        vendor("wxPayReturn.WxPay_Api");

        if (isset($out_trade_no) && $out_trade_no != "") {
            $input = new \WxPayOrderQuery();
            $input->SetOut_trade_no($out_trade_no);
            $config = new \WxPayConfig();
            $WxPayApi=new \WxPayApi();
            $re = $WxPayApi->orderQuery($config, $input);
            if ($re['result_code'] == 'SUCCESS') {
                if ($re['trade_state'] == 'SUCCESS') {
                    $re = array(
                        'status' => 200,
                        "msg" => "请求成功!",
                        "data" => array(
                            "out_trade_no" => $out_trade_no,
                            "wechat_trade_status" => "WAIT_BUYER_PAY"
                        )
                    );
                    return json_encode($re);
                }
            }
            $re = array(
                'status' => 400,
                "msg" => "查询失败",
            );
            return json_encode($re);
        } else {
            $re = array(
                'status' => 400,
                "msg" => "查询失败",
            );
            return json_encode($re);
        }

    }
}



